package com.livestocksolutions.ui;

import java.util.Date;

import com.livestocksolutions.R;
import com.livestocksolutions.intent.BaseActivity;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

/* -- Version History --
 *  1.0 : 13 August 2012 - Initial version.
 *  1.1 : 03 September 2012 - added setFocused() method and changed context from type Context to child
 *                          class BaseActivity.
 */

/**
 * A base class to create the layout for an Observation Object
 * @author Adam Parr
 * @version 1.1
 * 
 * @.modified 19 October 2012
 */
public class ViewObservationArrayAdapter extends BaseArrayAdapter {
	/**
	 * Size of the allowable array passed
	 */
	public static final int SIZE = 4;
	/**
	 * ID of the Observation within the String[position][{@value #ID}] array passed in the Constructor.
	 */
	public static final int ID = 0;
	
	/**
	 * Date of the Observation is within the String[position][{@value #DATE}] array passed in the Constructor.
	 */
	public static final int DATE = 1;
	
	/**
	 * Type this contact is within the String[position][{@value #TYPE}] array passed in the Constructor.
	 */
	public static final int TYPE = 2;
	/**
	 * Value of the contact position within the String[position][{@value #VALUE}] array passed in the Constructor.
	 * Note: Any additional array items are extra values and will be added if there is space for the given contact type
	 */
	public static final int VALUE = 3;
	
	public static final int TYPE_RESOLVED		= 0;
	public static final int TYPE_OBSERVATION 	= 1;
	public static final int TYPE_NONCRITICAL 	= 2;
	public static final int TYPE_WATCH 		= 3;
	public static final int TYPE_CRITICAL 	= 4;
	public static final int TYPE_URGENT	 	= 5;

	/**
	 * Flags to indicate if these values are valid or not.
	 * for {@link #isValid()} to return true, all of these
	 * flags must equal to true.
	 */
	private boolean validDATE = true;
	
	/**
	 * Constructor
	 * @param context The current context.
	 * @param resourceId The View to create
	 * @param values The Contacts to represent in the ListView.
	 */
	protected ViewObservationArrayAdapter(BaseActivity context, int resourceId, String[][] values){
		super(context, resourceId, values);
	}
	
	/**
	 * Constructor
	 * @param context The current context.
	 * @param values The Observation to represent in the ListView.
	 */
	public ViewObservationArrayAdapter(BaseActivity context, String[] values) {
		super(context, new String[][]{values});
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		setView(R.layout.item_observation);
		final View rowView = super.getView(position, convertView, parent);
		
		Spinner observationType = (Spinner) rowView.findViewById(R.id.observationPriority);
		EditText observationDescription = (EditText) rowView.findViewById(R.id.observation);
		EditText observationDate = (EditText) rowView.findViewById(R.id.observationDate);

		observationDescription.setText(values[position][VALUE]);
		observationDate.setText(context.format(context.parse(values[position][DATE])));
		observationDate.setEnabled(values[position][ID].equals("-1"));
		observationDate.addTextChangedListener(new TextWatcher() {
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
			public void onTextChanged(CharSequence s, int start, int before, int count) {}

			public void afterTextChanged(Editable s) {
				Date d = context.parse(s.toString());
				if(d!=null||s.toString().trim().equals("")){
					values[position][DATE]=s.toString().trim().equals("")?"":String.valueOf(d.getTime());
				}
				if(d==null || new Date().before(d)){
					validDATE=false;
				}else{
					validDATE=true;
				}
			}
		});
		
		observationType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view, int index, long id) {
				values[position][TYPE]=String.valueOf(index);
			}

			public void onNothingSelected(AdapterView<?> parent) {}
		});
		
		observationDescription.addTextChangedListener(new TextWatcher() {
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
			public void onTextChanged(CharSequence s, int start, int before, int count) {}

			public void afterTextChanged(Editable s) {
				values[position][VALUE]=s.toString();
			}
		});
		
		// Determine the Priority type this Observation is
		observationType.setSelection(Integer.valueOf(values[position][TYPE]==null?String.valueOf(TYPE_OBSERVATION):values[position][TYPE]));

		return rowView;
	}
	
	@Override
	public boolean isValid(){
		boolean dateNumber = true;
		Date d = null;
		// Update all values to the latest versions
		switch(textViewResourceId){
			case R.layout.item_cattle_new:
			if(!values[0][DATE].equals("")){
				d = context.parse(values[0][DATE]);
				if(d==null||new Date().before(d)){
					dateNumber = false;
				}
			}
		}
		
		// Do Validation
		validDATE=(values[0][DATE].equals("") || dateNumber);
		if(validDATE){
			return true; // Valid
		}
		
		// Something(s) are not valid, inform the user.
		String s = "";
		if(!validDATE){
			s=context.getString(d!=null?R.string.message_invalidDATEAfter:R.string.message_invalidDATE,context.getResources().getString(R.string.observation_date),DATEFORMATSTRINGSHORT);
			Toast.makeText(context,s, Toast.LENGTH_LONG).show();
		}
		return false;
	}
}